#!/bin/bash

# Slackware build script for libcapsimage

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# This software goes by many names. I've chosen to name the package
# after the shared library it installs.

# Currently this only exists for hatari to use. If you're going to use
# it as a dep for your SlackBuild:
# - You'll want to make sure it looks for headers in /usr/include/caps5,
#   probly with a -I option in CFLAGS.
# - The library is in the normal place, /usr/lib(64), so the only linker
#   flag you'll need (if it's not there already) is -lcapsimage.
# - Some software might want to call it -lcapsimg, just sed your Makefile
#   or whatever's needed.
# - The license for libcapsimage isn't GPL-compatible: it's "free for
#   non-commercial use". This shouldn't stop you or your users from
#   building and using packages, but you should note in your README if
#   the package is non-redistributable. I don't know if this affects
#   other licenses such as BSD or MIT.
# - The only other problem you might run into is that your software
#   might be written for version 4.x, which is totally different (this
#   script can't build it). If there's any interest, I'll do a build
#   for that as well.
# If you do use this as a dependency for your build, let me know, I'm
# interested.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=libcapsimage
VERSION=${VERSION:-5.1}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAM=spsdeclib

# Yo dawg, I herd you like zip files, so I put a zip file in your zip
# file, so you can unzip while you unzip.
INNERDIR=capsimg_source_linux_macosx
INNERZIP="$INNERDIR.zip"

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
unzip -a $CWD/${SRCNAM}_${VERSION}_source.zip $INNERZIP '*.txt'
unzip -a $INNERZIP
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

cd $INNERDIR/CAPSImg

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
sh ./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

make

# 'make install' is useless, so:
PKGLIB=$PKG/usr/lib$LIBDIRSUFFIX
mkdir -p $PKGLIB
install -s -m0755 $PRGNAM.so.$VERSION $PKGLIB
ln -s $PRGNAM.so.$VERSION $PKGLIB/$PRGNAM.so
# The .so.5 link is made by ldconfig at package install time.

# List of headers came from peeking at the windows binary build. Also it's
# the same set of headers hatari's readme.txt says it needs. I'm installing
# them where hatari expects to find them. Since this stuff was ported from
# Windows, it's littered with MS-specific __cdecl attributes which we
# don't need on Linux, so zap them.
mkdir -p $PKG/usr/include/caps5
cd ..
install -m0644 LibIPF/*.h Core/CommonTypes.h $PKG/usr/include/caps5
sed -i 's,__cdecl,,g' $PKG/usr/include/caps5/*.h

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cd ..
cp -a *.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

rm -f $PKG/usr/lib*/*.la

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
